/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.paroquiadegloria.ejc.view.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author jadson
 */
@ManagedBean
@RequestScoped
public class LineaChartMenuBean implements Serializable {

    private StreamedContent chart;

    /** Creates a new instance of LineChartMenuBean */
    public LineaChartMenuBean() {
        try {
            JFreeChart jfreechart = ChartFactory.createBarChart(null, null, null, createDataset(), PlotOrientation.HORIZONTAL, true, true, true);
            File chartFile = new File("dynamichart");
            ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 150, 120);
            chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
        } catch (IOException ex) {
            Logger.getLogger(LineaChartMenuBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private CategoryDataset createDataset() {
        final String series1 = "Entradas";
        final String series2 = "Despesas";

        final String type1 = "EJC 2012";

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(2304.0, series1, "");
        dataset.addValue(2480.0, series2, "");
        
        return dataset;  
    }

    public StreamedContent getChart() {
        return chart;
    }

    public void setChart(StreamedContent chart) {
        this.chart = chart;
    }
}
